package com.lzy.mall.order.config;

import cn.hutool.core.collection.CollUtil;
import com.lzy.mall.common.constant.MQConstant;
import com.lzy.mall.common.constant.OrderConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lzy
 * @date 2021/10/11 13:51
 */
@Configuration
public class MyMQConfig {

    @Autowired
    AmqpAdmin amqpAdmin;

    @Bean
    public Queue orderDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String, Object> arguments = new HashMap<>(3);
        arguments.put(MQConstant.XDLExchange, MQConstant.orderEventExchange);
        arguments.put(MQConstant.XDLRK, MQConstant.orderReleaseRK);
        arguments.put(MQConstant.XMTTL, MQConstant.XMTTL_TIME);
        return new Queue(MQConstant.orderDelayQueue,true,false,false,arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue(MQConstant.orderReleaseQueue,true,false,false);
    }

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(MQConstant.orderEventExchange, true, false);
    }
    @Bean
    public Binding orderCreateOrderBinding(){
        return new Binding(MQConstant.orderDelayQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.orderEventExchange,
                MQConstant.orderCreateRK,
                null);
    }
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding(MQConstant.orderReleaseQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.orderEventExchange,
                MQConstant.orderReleaseRK,
                null);
    }

    /**
     * 订单释放直接和库存释放绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding(MQConstant.stockReleaseQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.orderEventExchange,
                MQConstant.orderReleaseOtherRK + ".#",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue(MQConstant.orderSeckillReleaseQueue,true,false,false,null);
    }

    @Bean
    public Binding orderSeckillOrderQueueBinding(){
        return new Binding(MQConstant.orderSeckillReleaseQueue,
                Binding.DestinationType.QUEUE,
                MQConstant.orderEventExchange,
                MQConstant.orderSeckillRK,
                null);
    }
}
